const mongoUtil = require("../utils/mongo_util")
const collectionName = "American"
// 获取美剧
function American(tvList, callback) {
    const mmp = mongoUtil.getCollection(collectionName)
    mmp.then(collection => {
        collection.find().limit(Number(tvList.pagesize)).skip((Number(tvList.page) - 1) * Number(tvList.pagesize)).toArray(function (err, result) { // 返回集合中所有数据
            if (result.length) {
                // console.log("gggggg");
                callback(null, result)
                new Error()
            } else {
                // console.log("hhhhhhhh");
                callback(err ? err : {
                    reason: "没有数据"
                }, result)
            }
        });
    })
}
// 添加美剧
function addAmerican(AmericanTvObj, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({
            'doubanId': AmericanTvObj.doubanId
        }).toArray(function (err, result) {
            if (result.length > 0) {
                callback({
                    msg: "已存在"
                }, result)
                return
            }
            mongoUtil.getCollection(collectionName).then(collection => {
                collection.insertOne(AmericanTvObj, function (err, res) {
                    callback(err, res)
                });
            })
        })
    })
}
// 修改美剧信息
function patchAmerican(AmericanTvObj, callback) {
    const whereStr = {
        'doubanId': AmericanTvObj.doubanId,
    }
    const updateStr = {
        $set: {
            doubanId: AmericanTvObj.doubanId,
            title: AmericanTvObj.title,
            rate: AmericanTvObj.rate,
            poster: AmericanTvObj.poster
        }
    };
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.updateOne(whereStr, updateStr, function (err, res) {
            callback(err, res)
        });
    })
}
// 删除美剧
function deleteAmerican(AmericanTvId, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.deleteMany({
            doubanId: {
                $in: AmericanTvId
            }
        }, function (err, res) {
            callback(err, res)
        });
    })
}
module.exports = {
    American,
    addAmerican,
    patchAmerican,
    deleteAmerican
}